package com.koron.css2.serviceManage.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.TAuthorizationBean;
import com.koron.css2.serviceManage.bean.TAuthorizationBeanVO;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.TAuthorizationMapper;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

public class TAuthorizationAdd implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {

		MessageBean<String> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", String.class);

		try {
			TAuthorizationBeanVO tAuthorizationBeans = JsonUtils.objectToPojo(req.getData(), TAuthorizationBeanVO.class);
			Integer number = 0;

			String companyId = tAuthorizationBeans.getCompanyId();
			TAuthorizationMapper mapper = factory.getMapper(TAuthorizationMapper.class);
			List<TAuthorizationBean> gettAuthorizationBeans = tAuthorizationBeans.gettAuthorizationBeans();
			for (TAuthorizationBean tAuthorizationBean : gettAuthorizationBeans) {
				tAuthorizationBean.setCompanyCode(companyId);
				tAuthorizationBean.setCreateName(userInfo.getUserInfo().getName());
				TAuthorizationBean bean = mapper.getTAuthorizationByauthorizationValue(tAuthorizationBean);
				// 查询授权部门或人员是否已经存在
				// 不存在添加并计数
				if (bean != null) {
					// 存在跳过
					continue;
				}
				number += mapper.insertTAuthorization(tAuthorizationBean);
			}
			info.setData(number+"");
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		return info;
	}

}
